# ADX过滤
import talib
from PyQt5 import QtCore
# CTP行情库
from vnctpmd import *
import numpy as np
import globalvar

parlist = [['short', 3, 10, 1], ['long', 10, 30, 1]]


class MyFilter(QtCore.QThread):
    def __init__(self, period):
        super(MyFilter, self).__init__(period)
        self.high = []
        self.low = []
        self.close = []

    # 在策略文件中调用，获得返回值True或False
    # arg = [5, 20, 0, 0, 0, 0]
    # self.OnKline(kline, arg)

    def OnFilter(self, mddata, arg):
        if arg[0] <= 0 or arg[1] <= 0:
            return
        # TradingDay = klinedata.TradingDay.decode()
        # klinetime = klinedata.klinetime.decode()
        self.InstrumentID = mddata.InstrumentID.decode()
        # self.exchange=mddata.exchange.decode()
        self.high.append(float(mddata.high))
        self.low.append(float(mddata.low))
        self.close.append(float(mddata.close))
        try:
            float_high = [float(x) for x in self.high]
            float_low = [float(x) for x in self.low]
            float_close = [float(x) for x in self.close]
        except Exception as e:
            pass

        adx = talib.ADX(np.array(float_high), np.array(float_low), np.array(float_close), arg[0])
        # ADX(high, low, close[, timeperiod=?])
        # 当天
        #adx0 = adx[len(adx) - 1]
        print('~~~~~~~~~~~~~~~~~~adx: %d', adx)
        if adx > 25:
            return True
        else:
            return False
